package org.yoao.crm.DBManager;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.yoao.crm.bean.StaticBean;

public class JNDIPoolManagerImpl implements IPoolManagerService {
	private static DataSource ds;

	public JNDIPoolManagerImpl(){
		createDataSource();
	}

	public static void createDataSource(){
		if (ds == null){
			try{
				Context initContext = (Context)new InitialContext().lookup("java:/comp/env");
//				Context initContext = (Context)new InitialContext();
				if(initContext == null) System.out.println("无JNDI配置环境");
				ds = (DataSource) initContext.lookup(StaticBean.JNDINAME_VALUE);
			}
			catch(NamingException e){
				System.out.println("JNDI获取失败.");
				e.printStackTrace();
			}
			catch(Exception e){
				System.out.println("JNDI获取失败.");
				e.printStackTrace();
			}
		}
	}

	public Connection getConnection() throws SQLException {
		return ds.getConnection();
	}
}